# Create your views here.
import os
from django.contrib.auth import authenticate, login
from django.template import Context, loader
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from DB_WORK.urls import HIS_TEMPLATE_ROOT as TEMPLATE_ROOT
from DB_WORK.urls import PROJECT_ROOT
from DB_WORK.HIS.validation import *
from DB_WORK.HIS.models import *

from django.contrib.auth import logout
from DB_WORK.HIS.forms import * 

def sign_in(request):
	if (request.path == '/'):
		return render_to_response('HIS/index.html', {'login_status':''})

	username = request.GET.get(key = 'username')
	password = request.GET.get(key = 'password')
	user = authenticate(username=username, password=password)
	if user is not None:
		if user.is_active:
			login(request, user)
			role = get_user_role(user)
			return HttpResponseRedirect('/{0}/{1}'.format(role, user.username))
		else:
			return render_to_response('HIS/index.html', {'login_status':'The account is in_active.'})
	else:
		return render_to_response('HIS/index.html', {'login_status':'Unauthenticated user.'})

def logout_view(request):
	logout(request)
	return HttpResponseRedirect('/')
		
def department(request):
	user = request.user
	role = get_user_role(user)
	departments = fetch_all_departments()
	return render_to_response('HIS/{0}.html'.format(get_user_role(user)), {'whatfor':'department', 'departments':departments, 'username':user.username})

def medicine(request):
	user = request.user
	role = get_user_role(user)
	medicines = fetch_all_medicines()
	return render_to_response('HIS/{0}.html'.format(get_user_role(user)), {'whatfor':'medicine', 'medicines':medicines, 'username':user.username})

def patient(request, username):
	user = request.user
	if (user.is_authenticated() and user.is_active and role_match(user, 'patient')):
		if request.method == 'POST':
			personal_info = PatientForm(request.POST)
			if personal_info.is_valid():
				personal_info.save(user)
		p = Patient_Info.objects.get(patient_id=user.id)
		personal_info = PatientForm(instance=p)
		return render_to_response('HIS/patient.html', {'whatfor':'personal_info', 'personal_info':personal_info, 'username':user.username})

def doctor(request, username):
	user = request.user
	if (user.is_authenticated() and user.is_active and role_match(user, 'doctor')):
		if request.method == 'POST':
			personal_info = DoctorForm(request.POST)
			if personal_info.is_valid():
				personal_info.save(user)
		d = Doctor_Info.objects.get(doctor_id=user.id)
		personal_info = DoctorForm(instance=d)
		return render_to_response('HIS/doctor.html', {'whatfor':'personal_info', 'personal_info':personal_info, 'username':user.username})

def diagnose(request):
	user = request.user
	if (user.is_authenticated() and user.is_active and role_match(user, 'doctor')):
		if request.method == 'POST':
			diagnosis = DiagnosisForm(request.POST)
			if diagnosis.is_valid():
				diagnosis.save(user)
		diagnosis = DiagnosisForm()
		return render_to_response('HIS/doctor.html', {'whatfor':'diagnose', 'diagnosis':diagnosis, 'username':user.username})

def view_diagnosis(request, role, username):
	user = request.user
	role = get_user_role(user)
	diagnosis = None
	if role == 'doctor':
		diagnosis = Diagnosis.objects.filter(doctor_id=Doctor_Info.objects.get(doctor_id=user))
	if role == 'patient':
		diagnosis = Diagnosis.objects.filter(patient_id=Patient_Info.objects.get(patient_id=user))
	return render_to_response('HIS/{0}.html'.format(role), {'whatfor':'view_diagnosis', 'diagnosis':diagnosis, 'username':user.username})

def appointment(request):
	user = request.user
	if (user.is_authenticated() and user.is_active and role_match(user, 'doctor')):
		if request.method == 'POST':
			appointment = AppointmentForm(request.POST)
			if appointment.is_valid():
				appointment.save(user)
		appointment = AppointmentForm()
		return render_to_response('HIS/doctor.html', {'whatfor':'appointment', 'appointment':appointment, 'username':user.username})

def view_appointment(request, role, username):
	user = request.user
	role = get_user_role(user)
	appointment = None
	if role == 'doctor':
		appointment = Appointment.objects.filter(doctor_id=Doctor_Info.objects.get(doctor_id=user))
	if role == 'patient':
		appointment = Appointment.objects.filter(patient_id=Patient_Info.objects.get(patient_id=user))
	return render_to_response('HIS/{0}.html'.format(role), {'whatfor':'view_appointment', 'appointment':appointment, 'username':user.username})
